﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using engine;

namespace WindowsFormsApp1
{
    public class TijianTemplateFunc : TemplateFunc
    {
        public TijianTemplateFunc(FuncType funcType) : base(funcType)
        {
        }

        [RenderTemplateFunc(AppliedType = UnitType.Picture, Name = "ocr_risk")]
        public object OCRRisk(NodeBase node, PictureUnit pictureUnit)
        {
            // 返回图片，也同时设置风险数组值
            PictureUnit result = new PictureUnit();
            var arr = ParseRiskImage(pictureUnit.Image);
            return arr;
        }

        [RenderTemplateFunc(AppliedType = UnitType.Picture, Name = "create_risk_pic")]
        public object CreateRiskPic(PictureNode node, List<int> arr)
        {
            PictureUnit unit = new PictureUnit();
            unit.Source = node.Source;
            unit.Image = CreateNewRiskPic(arr);
            return unit;
        }

        private List<int> ParseRiskImage(Image image)
        {
            return new List<int>() { 10, 20, 30, 40, 50 };
        }

        private Image CreateNewRiskPic(List<int> arr)
        {
            var values = arr;

            Image imgTemp = new Bitmap("BaseTemplate/risk.png");
            Graphics g = Graphics.FromImage(imgTemp);

            List<SolidBrush> levels = new List<SolidBrush>();
            levels.Add(new SolidBrush(Color.FromArgb(143, 195, 31)));
            levels.Add(new SolidBrush(Color.FromArgb(0, 162, 154)));
            levels.Add(new SolidBrush(Color.FromArgb(243, 152, 0)));
            levels.Add(new SolidBrush(Color.FromArgb(195, 13, 35)));
            levels.Add(new SolidBrush(Color.FromArgb(96, 25, 134)));
            levels.Add(new SolidBrush(Color.FromArgb(96, 25, 134)));

            for (int i = 0; i < values.Count; i++)
            {
                int temp = values[i];
                int level = (int)(temp / 20);
                int x = 12 + i * 33;
                int height = (int)(205 * temp / 100);
                int y = 245 - height;
                int width = 16;
                g.FillRectangle(levels[level], x, y, width, height);
            }
            
            g.Dispose();
            return imgTemp;
        }
    }
}
